import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { list } from '@/views/website/TenderWebsiteManagement.api';
import { list as keywordList } from '@/views/keyword/TenderSecurityKeywordManagement.api';

import dayjs, { Dayjs } from 'dayjs';
import {render} from "@/utils/common/renderUtils";
type RangeValue = [Dayjs, Dayjs];

function getWebsiteList() {
  return list({ pageSize: 999 }).then((res) => {
    const list = res.records || [];
    return list.map((item) => {
      return { label: item.websiteName, value: item.websiteName };
    });
  });
}

function getKeywordList() {
  return keywordList({ pageSize: 999 }).then((res) => {
    const list = res.records || [];
    return list.map((item) => {
      return { label: item.keywordName, value: item.keywordName };
    });
  });
}

//列表数据
export const columns: BasicColumn[] = [
  {
    title: '招标信息标题',
    align: 'center',
    dataIndex: 'tenderTitle',
    slots: { customRender: 'tenderTitle' },
    width: 'auto',
    minWidth: 400,
  },
  {
    title: '发布日期',
    align: 'center',
    dataIndex: 'tenderDate',
    width: 160,
  },
  {
    title: '招标关键字',
    align: 'center',
    dataIndex: 'keywordName',
    width: 160,
  },
  {
    title: '来源网站',
    align: 'center',
    dataIndex: 'websiteName',
    width: 180,
  },
  {
    title: '归属项目',
    align: 'center',
    dataIndex: 'subProject',
    width: 160,
    // customRender: ({ text }) => {
    //   return render.renderDict(text, 'tender_keyword_project');
    // },
  },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  // { label: '招标单位', field: 'biddingUnit', component: 'Input' },
  { label: '招标信息标题', field: 'tenderTitle', component: 'Input' },
  {
    label: '来源网站',
    field: 'websiteName',
    component: 'ApiSelect',
    componentProps: {
      api: getWebsiteList,
    },
  },
  {
    label: '招标关键字',
    field: 'keywordName',
    component: 'ApiSelect',
    componentProps: {
      api: getKeywordList,
    },
  },
  {
    label: '发布日期',
    field: 'dateRage',
    component: 'RangePicker',
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      ranges: {
        当天: [dayjs().startOf('day'), dayjs()] as RangeValue,
        当月: [dayjs().startOf('month'), dayjs().endOf('month')] as RangeValue,
      },
      placeholder: ['开始时间', '结束时间'],
    },
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '招标单位',
    field: 'biddingUnit',
    component: 'Input',
  },
  {
    label: '招标单位行业',
    field: 'biddingUnitIndustry',
    component: 'Input',
  },
  {
    label: '招标预算',
    field: 'tenderAmount',
    component: 'InputNumber',
  },
  {
    label: '招标编码',
    field: 'biddingNum',
    component: 'Input',
  },
  {
    label: '招标方式',
    field: 'tenderType',
    component: 'Input',
  },
  {
    label: '投标开始时间',
    field: 'startTime',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    label: '投标截止时间',
    field: 'endTime',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    label: '开标时间',
    field: 'tenderDate',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    label: '中标单位',
    field: 'biddingWinner',
    component: 'Input',
  },
  {
    label: '代理机构',
    field: 'agency',
    component: 'Input',
  },
  {
    label: '招标信息标题',
    field: 'tenderTitle',
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入招标信息标题!' }];
    },
  },
  {
    label: '招标详情',
    field: 'tenderDesc',
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入招标详情!' }];
    },
  },
  {
    label: '招标网站地址',
    field: 'websiteUrl',
    component: 'Input',
    dynamicRules: ({ model, schema }) => {
      return [{ required: true, message: '请输入招标网站地址!' }];
    },
  },
  {
    label: '爬取时间',
    field: 'crawlTime',
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];

// 高级查询数据
export const superQuerySchema = {
  biddingUnit: { title: '招标单位', order: 0, view: 'text', type: 'string' },
  biddingUnitIndustry: { title: '招标单位行业', order: 1, view: 'text', type: 'string' },
  tenderAmount: { title: '招标预算', order: 2, view: 'number', type: 'number' },
  biddingNum: { title: '招标编码', order: 3, view: 'text', type: 'string' },
  tenderType: { title: '招标方式', order: 4, view: 'text', type: 'string' },
  startTime: { title: '投标开始时间', order: 5, view: 'datetime', type: 'string' },
  endTime: { title: '投标截止时间', order: 6, view: 'datetime', type: 'string' },
  tenderDate: { title: '开标时间', order: 7, view: 'datetime', type: 'string' },
  biddingWinner: { title: '中标单位', order: 8, view: 'text', type: 'string' },
  agency: { title: '代理机构', order: 9, view: 'text', type: 'string' },
  tenderTitle: { title: '招标信息标题', order: 10, view: 'text', type: 'string' },
  tenderDesc: { title: '招标详情', order: 11, view: 'text', type: 'string' },
  websiteName: { title: '招标网站名称', order: 12, view: 'text', type: 'string' },
  crawlTime: { title: '爬取时间', order: 13, view: 'datetime', type: 'string' },
};

/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
